package com.findbus.activity;

import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;

import com.entity.MessageInfor;
import com.findbus.project.R;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.util.tool.ApiConnHelp;
import com.util.tool.MD5Util;
import com.util.tool.ToastHelp;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

public class RegisteActivity extends FragmentActivity {
	@ViewInject(R.id.a_re_check_isArge)
	private CheckBox isArge;
	@ViewInject(R.id.a_re_edit_phone)
	private EditText phone;
	@ViewInject(R.id.a_re_edit_yz)
	private EditText yz;
	@ViewInject(R.id.a_re_edit_pass)
	private EditText pass;
	@ViewInject(R.id.a_re_edit_passa)
	private EditText passa;
	private Resources resources;
	private Handler handler;
	private String phones;
	private String yzmm;
	private MessageInfor mm;

	@SuppressLint("HandlerLeak")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_registe);
		resources = getResources();
		handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				switch (msg.what) {
				case 0:
					mm = (MessageInfor) msg.obj;
					if (mm.getFlag() == 1) {
						ToastHelp.toast(RegisteActivity.this, resources.getString(R.string.re_suss));
						result();
					} else if (mm.getFlag() == 0) {
						ToastHelp.toast(RegisteActivity.this, resources.getString(R.string.re_err));
						initEdit();
						return;
					} else {
						ToastHelp.toast(RegisteActivity.this, resources.getString(R.string.network_err));
						initEdit();
						return;
					}
					break;
				case 1:
					mm = (MessageInfor) msg.obj;
					if (mm.getFlag() == 1)
						yzmm = mm.getCodes();
					else {
						ToastHelp.toast(RegisteActivity.this, resources.getString(R.string.network_err));
						return;
					}
					break;
				case 2:

					break;
				}
			}

			private void initEdit() {
				// TODO Auto-generated method stub
				phone.setText("");
				pass.setText("");
				passa.setText("");
				yz.setText("");
			}
		};
		initView();
	}

	private void initView() {
		// TODO Auto-generated method stub
		ViewUtils.inject(this);
	}

	private void result() {
		Intent intent = new Intent();
		Bundle bundle = new Bundle();
		bundle.putString("phone", phones);
		intent.putExtras(bundle);
		setResult(0, intent);
		finish();
	}

	public void clickButton(View v) {
		Runnable run = null;
		phones = phone.getText() + "";
		switch (v.getId()) {
		case R.id.a_re_button_yz:
			if (TextUtils.isEmpty(phones)) {
				ToastHelp.toast(this, resources.getString(R.string.re_nophone));
				return;
			}
			ToastHelp.toast(this, resources.getString(R.string.re_onyz));
			run = new Runnable() {

				@Override
				public void run() {
					Message mes = handler.obtainMessage();
					mes.obj = ApiConnHelp.messageTest(phones);
					mes.what = 1;
					handler.sendMessage(mes);
				}
			};
			break;
		case R.id.a_re_text_xy:
			startActivity(new Intent(this,WebActivity.class));
			return;
		case R.id.a_re_button_re:
			String yzs = yz.getText() + "";
			final String passs = pass.getText() + "";
			String passss = passa.getText() + "";
			String msg = null;
			msg = TextUtils.isEmpty(yzs) ? resources.getString(R.string.re_noyz)
					: TextUtils.isEmpty(passs) ? resources.getString(R.string.re_nopass)
							: TextUtils.isEmpty(passss) ? resources.getString(R.string.re_nopassa)
									: !yzs.equals(yzmm) ? resources.getString(R.string.re_yzerr)
											: isArge.isChecked() ? null : resources.getString(R.string.re_isa);
			if (msg != null) {
				ToastHelp.toast(this, msg);
				return;
			}

			run = new Runnable() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					Message mes = handler.obtainMessage();
					mes.obj = ApiConnHelp.register("", phones + "", MD5Util.generatePassword(passs), "", "", "", "", "",
							"", "");
					mes.what = 0;
					handler.sendMessage(mes);
				}
			};
			break;
		}
		if (run != null)
			new Thread(run).start();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK)
			result();
		return super.onKeyDown(keyCode, event);
	}
}
